/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mb.webserver.handler;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mb.webserver.inMemory.MBServerConfigProcess;

/**
 *
 * @author thanh.nguyenchi
 */
public class MbServerConfig extends BaseServlet{   
    
    public static final String PROPERTIES_SERVER_CONFIG_FILE = "server.cfg";
    public static final String PROPERTIES_SERVER_LINK = "SERVER_LINK";
    public static final String PROPERTIES_APP_VERSION = "APP_VERSION";
    
    String serverLink = "";
    String appVersion = "";
            
    //load server config from file
    public MbServerConfig(){
        super(); 
        loadConfig();
    }
    
    void loadConfig(){
        FileInputStream objConfigStream = null;
        try{
            //open properties file to get configuration
            log("Loading config from file: " + PROPERTIES_SERVER_CONFIG_FILE + " ...");
            Properties myProps = new Properties();
            objConfigStream = new FileInputStream(PROPERTIES_SERVER_CONFIG_FILE);
            myProps.load(objConfigStream);        
            
            //server link
            serverLink = myProps.getProperty(PROPERTIES_SERVER_LINK);
            log("Server link = " + serverLink);
            
            //version link
            appVersion = myProps.getProperty(PROPERTIES_APP_VERSION);
            log("App version = " + appVersion);
                       
        }
        catch(Exception ex){
            log(ex.getMessage());    
        }
        finally{
            try{
                if(objConfigStream!= null) objConfigStream.close(); // better in finally block
            }
            catch(Exception exx){}	
        }
    }
    
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException,IOException
    {         
        String sOut = "";
        //api/loadConfig?version={1}&lang={2}&os={3}&osVersion={4}&engine={5}&engineVersion={6}&callback={7}
        String version          = request.getParameter("version");              // check 
        String lang             = request.getParameter("lang");                 // log
        String osVersion        = request.getParameter("osVersion");            // log
        String engine           = request.getParameter("engine");               // log
        String engineVersion    = request.getParameter("engineVersion");        // log
        String callback         = request.getParameter("callback");             // check 
        
        StringBuffer sbLog = new StringBuffer();
        sbLog.append("MbConfig ==> [v:" + version);
        sbLog.append("] [l:" + lang + "] oS:[" + osVersion);
        sbLog.append("] [e:" + engine + "] [eV:" + engineVersion);
        sbLog.append("] [c:" + callback + "] from [ip:" + request.getRemoteAddr() + "]");
        log(sbLog.toString());
        
        // call bussiness
        MBServerConfigProcess objProcess = new MBServerConfigProcess(serverLink,appVersion,lang);
        if ((callback != null) && (callback.length() > 1)){
            sOut = callback + "(" + objProcess.getResult() + ")";
        }
        else{
            // return no callback
            sOut = objProcess.getResult();
        }                
        
        response.setContentType("application/json; charset=UTF-8"); 
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(sOut);
    }    
    
}
